<?php
if (!defined("BASEPATH")) exit("No direct script access allowed");

class Sys_log extends MY_Model {

	private static $sysCnt = FALSE; // volt e adatbazis iras, daemon-t triggerel

	function __construct() {
		parent::__construct();
		$this->tableName = "sys_log";
		$this->prefix = "log";
	}
	
	function getSysCnt() {
		return self::$sysCnt;
	}

	function setSysCnt($val) {
		self::$sysCnt = $val;
	}
	
	function getSys_log($where = NULL, $start = NULL, $limit = NULL, $fields = NULL, $query = NULL, $order = "logDate", $dir = "ASC") {

		$fieldsQuery = Common::fieldsquery($fields, $query);

		$qs =	"
					select
						log_id, log_usr_id, log_rec_id, log_table, log_type, log_date, log_changes
					from
						".$this->db->dbprefix.$this->tableName."
					where
						1 = 1 ".((!is_null($where))?(" and ".$where):(""))." ".(($fieldsQuery)?(" and (".$fieldsQuery.") "):(""))."
					order by
						".Common::underscore($order)." ".$dir."
					".((!is_null($start))?(" limit ".$start.",".$limit):(""))."
					";
		$result = $this->db->query($qs);

		$getSys_log = array();
		if($result->num_rows()) {
			foreach ($result->result() as $row) {

				$getSys_log[] = array(
					"logId" => $row->log_id,
					"logUsrId" => $row->log_usr_id,
					"logRecId" => $row->log_rec_id,
					"logTable" => $row->log_table,
					"logType" => $row->log_type,
					"logDate" => $row->log_date,
					"logChanges" => str_replace(array("<", ">"), array("&lt;", "&gt;"), $row->log_changes),
				);

			}
		}

		return $getSys_log;
	}

	function getSys_logCount($where = NULL, $fields, $query) {

		$fieldsQuery = Common::fieldsquery($fields, $query);

		$qs =	"
					select
						count(*) as cnt
					from
						".$this->db->dbprefix.$this->tableName."
					where
						1 = 1 ".((!is_null($where))?(" and ".$where):(""))." ".(($fieldsQuery)?(" and (".$fieldsQuery.") "):(""))."
					";
		$result = $this->db->query($qs);

		if($result->num_rows()) {
			foreach ($result->result() as $row) {
				return $row->cnt;
			}
		}
	}

}